﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.Model;
using Register.IDAL;
using Register.Command;
using Register.DBUtility;
using System.Data.SqlClient;
using System.Data;
namespace Register.DAL
{
    /// <summary> 
    /// FileName: HospitalInstallDAL.cs    
    /// CLRVersion: 4.0.30319.269    
    /// Author: DoubleZ    
    /// Corporation:     
    /// Description:     
    /// DateTime: 2012/7/6 10:00:19    
    /// </summary>
   public class HospitalInstallDAL:IHospitalInstallDAL
   {
       private string tableName = "System_hospitalInstall";
       #region 添加医院配置
       public int AddHospitalInstall(HospitalInstall hospitalInstall)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("insert into System_hospitalInstall(");
           strSql.Append("HospitalInstall_ID,Hospital_ID,HosptialInstall_StartRegistrationTime,HosptialInstall_BeforeDay,HosptiallInstall_WorkTime,HosptiallInstall_AppointmentPhone,HospitalInstall_AddTime,HospitalInstall_Recoder)");
           strSql.Append(" values (");
           strSql.Append("@HospitalInstall_ID,@Hospital_ID,@HosptialInstall_StartRegistrationTime,@HosptialInstall_BeforeDay,@HosptiallInstall_WorkTime,@HosptiallInstall_AppointmentPhone,@HospitalInstall_AddTime,@HospitalInstall_Recoder)");
           SqlParameter[] parameters = {
					new SqlParameter("@HospitalInstall_ID", SqlDbType.Char,5),
					new SqlParameter("@Hospital_ID", SqlDbType.Char,5),
					new SqlParameter("@HosptialInstall_StartRegistrationTime", SqlDbType.DateTime),
					new SqlParameter("@HosptialInstall_BeforeDay", SqlDbType.Int),
					new SqlParameter("@HosptiallInstall_WorkTime", SqlDbType.VarChar,100),
					new SqlParameter("@HosptiallInstall_AppointmentPhone", SqlDbType.VarChar,20),
					new SqlParameter("@HospitalInstall_AddTime", SqlDbType.DateTime),
					new SqlParameter("@HospitalInstall_Recoder", SqlDbType.VarChar,20),
                                       };
           parameters[0].Value = hospitalInstall.HospitalInstall_ID;
           parameters[1].Value = hospitalInstall.Hospital_ID;
           parameters[2].Value = hospitalInstall.HosptialInstall_StartRegistrationTime;
           parameters[3].Value = hospitalInstall.HosptialInstall_BeforeDay;
           parameters[4].Value = hospitalInstall.HosptiallInstall_WorkTime;
           parameters[5].Value = hospitalInstall.HosptiallInstall_AppointmentPhone;
           parameters[6].Value = hospitalInstall.HospitalInstall_AddTime;
           parameters[7].Value = hospitalInstall.HospitalInstall_Recoder;
          
          int flag=DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
          if (flag > 0)
          {
              string strSql1 = "update System_hospitalInfo set HospitalInstall_ID='" + hospitalInstall.HospitalInstall_ID + "' where Hospital_ID='" + hospitalInstall.Hospital_ID + "'";
              return (int)DbHelperSQL.ExecuteSql(strSql1);
          }
          else return -1;
       }
       #endregion
       #region 获取配置列表
      public List<Model.HospitalInstall> GetPagedHospitalInstall(int size, int pageIndex, string where) {
          return DataSetToList(DbHelperSQL.getPagedData(" * ", "HospitalInstall_AddTime", where, tableName, OrderBy.DESC, pageIndex, size));
       }

      public int GetHospitalInstallCount(string where) {
          int count = 0;
          StringBuilder str = new StringBuilder();
          str.Append(" select count(*) from ");
          str.Append(tableName);
          if (where != null)
          {
              str.Append(" where ");
              str.Append(where);
              count = (int)DbHelperSQL.GetSingle(str.ToString());
          }
          else
          {
              count = (int)DbHelperSQL.GetSingle(str.ToString());
          }
          return count;
      }
      private List<Model.HospitalInstall> DataSetToList(DataSet ds)
      {
          List<Model.HospitalInstall> list = new List<Model.HospitalInstall>();
          foreach (DataRow dr in ds.Tables[0].Rows)
          {
                HospitalInstall install = new HospitalInstall();
                install.Hospital_ID = (string)dr["Hospital_ID"];
                install.HospitalInstall_AddTime = (DateTime)dr["HospitalInstall_AddTime"];
                install.HospitalInstall_ID = (string)dr["HospitalInstall_ID"];
                install.HospitalInstall_Recoder = (string)dr["HospitalInstall_Recoder"];
                install.HosptialInstall_BeforeDay = (int)dr["HosptialInstall_BeforeDay"];
                install.HosptialInstall_StartRegistrationTime = (DateTime)dr["HosptialInstall_StartRegistrationTime"];
                install.HosptiallInstall_AppointmentPhone = (string)dr["HosptiallInstall_AppointmentPhone"];
                install.HosptiallInstall_WorkTime = (string)dr["HosptiallInstall_WorkTime"];

              list.Add(install);
          }
          return list;
      }
       #endregion
       #region 根据医院ID获取医院名称
     public string GetHospitalNameById(string hospitalID) { 
      string strSql = "select Hospital_Name from System_hospitalInfo where Hospital_ID='"+hospitalID+"'";
     DataSet ds = DbHelperSQL.Query(strSql);
     string hospitalName = "无";
     if (ds != null)
     {
         hospitalName = ds.Tables[0].Rows[0]["Hospital_Name"].ToString();

     }
     return hospitalName;
        }
      
       #endregion
     #region 删除医院配置
     public int DeleteHospitalInstall(String hospitalInstallID)
     {
         StringBuilder strSql = new StringBuilder();
         strSql.Append("delete from System_hospitalInstall ");
         strSql.Append(" where HospitalInstall_ID=@HospitalInstall_ID ");
         SqlParameter[] parameters = {
					new SqlParameter("@HospitalInstall_ID", SqlDbType.Char,5)};
         parameters[0].Value = hospitalInstallID;

         int flag = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
         if (flag > 0)
         {
             string strSql1 = "update System_hospitalInfo set HospitalInstall_ID=null where HospitalInstall_ID='" +hospitalInstallID + "'";
             return DbHelperSQL.ExecuteSql(strSql1.ToString());
         }
         else return -1;
     }
     #endregion
       #region 更新配置
    public int UpdateHospitalInstall(HospitalInstall hospitalInstall) {
        StringBuilder strSql = new StringBuilder();
        strSql.Append("update System_hospitalInstall set ");
        strSql.Append("HosptialInstall_StartRegistrationTime=@HosptialInstall_StartRegistrationTime,");
        strSql.Append("HosptialInstall_BeforeDay=@HosptialInstall_BeforeDay,");
        strSql.Append("HosptiallInstall_WorkTime=@HosptiallInstall_WorkTime, ");
        strSql.Append("HosptiallInstall_AppointmentPhone=@HosptiallInstall_AppointmentPhone,");
        strSql.Append("HospitalInstall_AddTime=@HospitalInstall_AddTime,");
        strSql.Append("HospitalInstall_Recoder=@HospitalInstall_Recoder");
        strSql.Append(" where HospitalInstall_ID=@HospitalInstall_ID");
        SqlParameter[] parameters = {	
                  	new SqlParameter("@HosptialInstall_StartRegistrationTime", SqlDbType.DateTime),
					new SqlParameter("@HosptialInstall_BeforeDay", SqlDbType.Int),
					new SqlParameter("@HosptiallInstall_WorkTime", SqlDbType.VarChar,100),
					new SqlParameter("@HosptiallInstall_AppointmentPhone", SqlDbType.VarChar,20),
					new SqlParameter("@HospitalInstall_AddTime", SqlDbType.DateTime),
					new SqlParameter("@HospitalInstall_Recoder", SqlDbType.VarChar,20),
                        new SqlParameter("@HospitalInstall_ID", SqlDbType.Char,5)
                                    };
       
        parameters[0].Value = hospitalInstall.HosptialInstall_StartRegistrationTime;
        parameters[1].Value = hospitalInstall.HosptialInstall_BeforeDay;
        parameters[2].Value = hospitalInstall.HosptiallInstall_WorkTime;
        parameters[3].Value = hospitalInstall.HosptiallInstall_AppointmentPhone;
        parameters[4].Value = hospitalInstall.HospitalInstall_AddTime;
        parameters[5].Value = hospitalInstall.HospitalInstall_Recoder;
        parameters[6].Value = hospitalInstall.HospitalInstall_ID;
        return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
     }
       #endregion



   }
}
